﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mediatheque.Areas.Admin.Models;

namespace mediatheque.Areas.Admin.Controllers
{
    public class BooksController : Controller
    {
        //
        // GET: /Admin/Book/
        private mediathequeEntities db = new mediathequeEntities();
        private Book myBook = new Book();


        public ActionResult Index()
        {
            return View();
        }

        public ActionResult BookList()
        {
            return View("bookList", myBook);
        }

        public ActionResult AddBook(FormCollection data)
        {

            if (data["date_edition"].Equals("") || data["date_publication"].Equals(""))
            {
                ViewData["error_message"] = "Veuillez remplir tous les champs";
                return View("AddBook", myBook);

            }
            else
            {
                oeuvre o = new oeuvre
                    {
                        titre = Request.Form["titre"],
                        auteur = int.Parse(Request.Form["auteur"]),
                        date_edition = DateTime.Parse(Request.Form["date_edition"]),
                        date_publication = DateTime.Parse(Request.Form["date_publication"]),
                        nb_pages = int.Parse(Request.Form["nb_pages"]),
                        type = "livre"

                    };
                if (TryUpdateModel(o))
                {
                    db.oeuvres.AddObject(o);
                    db.SaveChanges();
                    ViewData["message"] = "Le livre a bien été enregistré";
                    return View("bookList", myBook);
                }
                else
                {
                    return View("AddBook", myBook);
                }


            }
        }

        public ActionResult EditBook(FormCollection data, int id)
        {
            var o = db.oeuvres.First(i => i.id == id);
            try
            {
                if (data.Count > 4)
                {
                    o.titre = Request.Form["titre"];
                    o.auteur = int.Parse(Request.Form["auteur"]);
                    o.date_edition = DateTime.Parse(Request.Form["date_edition"]);
                    o.date_publication = DateTime.Parse(Request.Form["date_edition"]);
                    o.nb_pages = int.Parse(Request.Form["nb_pages"]);
                    o.type = "livre";
                    db.SaveChanges();
                    ViewData["message"] = "Le livre a bien été modifié";
                    return View("bookList", myBook.getBookById(id));

                }
                else
                {
                    ViewData["error_message"] = "La modification a échouée Veuillez recommencer";
                    return View("editBook", myBook.getBookById(id));

                }
            }
            catch
            {
                ViewData["errormessage"] = "La modification a échouée Veuillez recommencer";
                return RedirectToAction("editBook", new { id = o.id });
            }

        }
    }
}
    